package com.yc.cloud.portal.runnable;

import cn.hutool.json.JSONUtil;
import com.yc.cloud.entity.AnswerRecord;
import com.yc.cloud.portal.service.AnswerRecordService;
import com.yc.cloud.portal.vo.AnswerVo;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;

//保存答题记录
@Slf4j
public class SaveAnswerRecordRunnable implements Runnable {

    private final AnswerVo answerVo;
    private final AnswerRecordService answerRecordService;


    public SaveAnswerRecordRunnable(AnswerVo answerVo, AnswerRecordService answerRecordService) {
        this.answerVo = answerVo;
        this.answerRecordService = answerRecordService;
    }

    @Override
    public void run() {
        val answerRecord = new AnswerRecord();
        //拷贝一些算法花费的时间
        BeanUtils.copyProperties(answerVo, answerRecord);
        //设置ai回答的内容
        answerRecord.setSessionId(answerVo.getSessionId());
        //设置问题内容
        answerRecord.setQuestionContent(answerVo.getQuestionContent());
        //设置回答内容
        answerRecord.setAnswerContent(answerVo.getContent());

        answerRecord.setCreateBy(answerVo.getUserId());
        answerRecord.setId(null);
        //计算总耗时
        var ttsCostTime = answerVo.getTtsCostTime() != null ? answerVo.getTtsCostTime() : 0L;
        var nlpCostTime = answerVo.getNlpCostTime() != null ? answerVo.getNlpCostTime() : 0L;
        var iatCostTime = answerVo.getIatCostTime() != null ? answerVo.getIatCostTime() : 0L;
        val totalCostTime = ttsCostTime + nlpCostTime + iatCostTime;
        answerRecord.setTotalCostTime(totalCostTime);
        // answerRecord.setSupplier(ai);
        //设置提问者信息
        log.info("保存答题记录：{}", JSONUtil.toJsonPrettyStr(answerRecord));
        answerRecordService.save(answerRecord);
    }
}
